#自定义模板过滤器
from django import template
from apps.blog.models import Post,Brief
from  taggit.models import Tag
register = template.Library()
@register.inclusion_tag('blog/latest_post.html')
def show_latest_posts(count=5):
    latest_posts=Post.published.order_by('-publish')[:count]
    return {'latest_posts':latest_posts}

import markdown
@register.filter(name='markdown')
def markdown_format(text):
    return markdown.markdown(text,
                             extensions=[
                                 # 包含 缩写、表格等常用扩展
                                 'markdown.extensions.extra',
                                 # 语法高亮扩展
                                 'markdown.extensions.codehilite',
                                 # 允许我们自动生成目录
                                 # 'markdown.extensions.toc',
                             ])
# 归档模板标签
@register.inclusion_tag('blog/pigeonhole_post.html')
def pigeonhole_list_posts():
    #        - year : 年-01-01 d.year d.month
    #         - month: 年-月-01
    #         - day  : 年-月-日
    latest_posts= Post.objects.dates('created', 'month', order='DESC')
    return {'latest_posts':latest_posts}

#标签云
@register.inclusion_tag('blog/taggs_yun.html')
def taggst_list():
    taggst_post=Tag.objects.all()
    return {'taggst_post':taggst_post}

#定制过滤器用于搜索使用
@register.filter
def subtract(value,num):
    return int(int(value)-int(num))

#背景图
@register.simple_tag
def body_img():
    brief=Brief.objects.all().first()
    if brief:
        brief=brief.postbd_img
    else:
        brief='#'
    return brief

#背景图_透明度
@register.simple_tag
def body_img_opacity():
    brief=Brief.objects.all().first()
    if brief:
        brief = brief.post_opacity
    return brief